package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.bar.TitleBar;
import com.hjq.base.BaseAdapter;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.db.LLSceneDBManager;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.manager.SceneOperateManager;
import com.qiantu.phone.ui.adapter.LinkageAdapter;
import com.qiantu.phone.ui.adapter.RoomManagerSceneAdapter;
import com.qiantu.phone.ui.fragment.SceneFragment;
import com.qiantu.phone.utils.SceneDetailUtil;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class DeviceSceneActivity  extends AppActivity {
    private RecyclerView sceneList;
    private Intent intent = null;
    private LinearLayout ll_Noequipment;
    private RoomManagerSceneAdapter sceneAdapter;
    private List<SceneBean> allScene;
    private SceneOperateManager mSceneOperateManager;
    private String deviceSerialNo;

    @Log
    public static void start(Context context, String serialNo, int deviceGroup) {
        Intent intent = new Intent(context, DeviceSceneActivity.class);
        intent.putExtra("serialNo", serialNo);
        intent.putExtra("deviceGroup", deviceGroup);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_device_scene;
    }

    @Override
    protected void initView() {
        sceneList = findViewById(R.id.scene_list);
        ll_Noequipment = findViewById(R.id.ll_Noequipment);
        initRecyclerView();
    }

    private void initRecyclerView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        sceneList.setLayoutManager(gridLayoutManager);
        sceneAdapter = new RoomManagerSceneAdapter(getContext())
                .setSelectAct(false)
                .setShowFloorRoom(true)
                .setSingleSelect(false);

        sceneAdapter.setOnChildClickListener(R.id.setting, new BaseAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(RecyclerView recyclerView, View childView, int position) {
                SceneDetailUtil.getSceneDetailAndToEdit(DeviceSceneActivity.this, DeviceSceneActivity.this, sceneAdapter.getData().get(position).getSceneSerialNo(),deviceSerialNo);
            }
        });
        sceneAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                SceneBean scene = sceneAdapter.getData().get(position);
                if(mSceneOperateManager == null){
                    mSceneOperateManager = new SceneOperateManager(getContext());
                }
                mSceneOperateManager.onClick(itemView, scene.getSceneSerialNo());
            }
        });

        sceneList.setAdapter(sceneAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void initData() {
        if (AppApplication.getInstance().getCurHouseInfo() == null) {
            toast(R.string.house_info_null);
            return;
        }
        deviceSerialNo = getIntent().getStringExtra("serialNo");
        int deviceGroup = getIntent().getIntExtra("deviceGroup",0);
        HttpCallback<HttpData<List<SceneBean>>> callback = new HttpCallback<HttpData<List<SceneBean>>>(this){
            @Override
            public void onStart(Call call) {
            }

            @Override
            public void onSucceed(HttpData<List<SceneBean>> result) {
                allScene = result.getData();
                if (allScene == null || allScene.size() == 0) {
                    ll_Noequipment.setVisibility(View.VISIBLE);
                    sceneList.setVisibility(View.GONE);
                } else {
                    ll_Noequipment.setVisibility(View.GONE);
                    sceneList.setVisibility(View.VISIBLE);
                    sceneAdapter.setData(allScene);
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
            }
        };
        if(deviceGroup == 1){
            LLHttpManager.getSceneByDeviceGroupSerialNo(this, deviceSerialNo, AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo(), callback);
        }else{
            LLHttpManager.getSceneByDeviceSerialNo(this, deviceSerialNo, AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo(), callback);
        }
    }
}
